﻿// pch.cpp: 与预编译标头对应的源文件

#include "pch.h"
#include "NetManager.h"
#include "WrapperSpi.h"
#include "SetFunc.h"
#include "LFConstants.h"
#include <thread>
#include <optional>
// 当使用预编译的头时，需要使用此源文件，编译才能成功。



double Add(double a, double b)
{
	return a + b;
}

void print() {

    while (true)
    {
        ::Sleep(300);

    }
}
std::thread  g_thread;
bool g_call_once = true;


//#define DEBUG
#ifdef   DEBUG

#endif //   DEBUG

void InitNet(const char* moduleTag, CAccountDetail acct, CAccountDetail credit) {

    
#ifdef   DEBUG
    FileAdapter::GetInstance().flush_to_file("policy.log", "define DEBUG\n");
    CAccountDetail detailS;
    detailS.m_dAvailable = 1000;
    testf(detailS, 3);
    return;
#endif //   DEBUG

    if (!g_call_once) return;
    g_call_once = false;
	g_thread = std::thread{ &NetManager<Spi, OrderPolicy>::init,  NetManager<Spi, OrderPolicy>::GetInstance() };
    //g_thread = std::thread{ print };
    g_thread.detach();
	//NetManager<Spi, OrderPolicy>::GetInstance().init();
	NetManager<Spi, OrderPolicy>::GetInstance().set_config(moduleTag, acct, credit);
	CAccountDetail detail;
	detail.m_dAvailable = 1000;
	testf(detail,3);

}



/*


enum  __declspec(dllexport)  EXtFWTaskStatus
{
    XTFW_TASK_STATUS_UNKNOWN = 0,                     ///< 未知状态
    XTFW_TASK_STATUS_WAITING,                         ///< 等待
    XTFW_TASK_STATUS_COMMITING,                       ///< 提交
    XTFW_TASK_STATUS_RUNNING,                         ///< 执行中
    XTFW_TASK_STATUS_PAUSE,                           ///< 暂停
    XTFW_TASK_STATUS_CANCELING_DEPRECATED,            ///< 撤销中
    XTFW_TASK_STATUS_EXCEPTION_CANCELING_DEPRECATED,  ///< 异常撤销中
    XTFW_TASK_STATUS_COMPLETED,                       ///< 完成
    XTFW_TASK_STATUS_CANCELED,                        ///< 已撤
    XTFW_TASK_STATUS_REJECTED,                        ///< 打回
    XTFW_TASK_STATUS_EXCEPTION_CANCELED,              ///< 异常终止
    XTFW_TASK_STATUS_DROPPED,                         ///< 放弃，目前用于组合交易中，放弃补单
    XTFW_TASK_STATUS_FORCE_CANCELED_DEPRECATED,       ///< 强制终止

};




*/




#//define PRINT_TRADE
int g_local_id = 1;
void rtn_cancel_order(const CTradeAction& data, const char* accountKey, int nRequestId, const XtError& error) {

    FileAdapter::GetInstance().flush_to_file("policy.log", "[on_order] " + string("[requestId]") + to_string(nRequestId) + "msg \n");

   // return;

    FileAdapter::GetInstance().flush_to_file("policy.log", "on_order " + string("[status]")  + to_string(data.m_eStatus) + "msg \n");
    FileAdapter::GetInstance().flush_to_file("policy.log", "on_order nano :" + to_string(InitData::GetInstance().ns.rdns()) + "\n");
    FileAdapter::GetInstance().flush_to_file("policy.log", "on_order msg detail :" + string("[request]") + to_string(nRequestId) + string("[group_id] ") + to_string(data.m_nGroupID) + "[task_id] " + to_string(data.m_nTaskId) + "---" + string(data.m_strMsg) + "\n");
    char buf[1024] = {};
    if (data.m_eStatus == XTFW_TASK_STATUS_RUNNING) {
        LFRtnOrderField rtn_order;

        auto& order_map = NetManager<Spi, OrderPolicy>::GetInstance().m_order_map;
        auto &task_map = NetManager<Spi, OrderPolicy>::GetInstance().m_task_map;

        if (order_map.find(nRequestId) != order_map.end()) {
            //string key = to_string(order_map[nRequestId].first) + to_string(order_map[nRequestId].second);
            string key = to_string(data.m_nTaskId) + to_string(data.m_nGroupID);
            task_map[key] = { order_map[nRequestId].first , order_map[nRequestId].second};
        }

     //   rtn_order.order_id = order_map[nRequestId].first;
     //   rtn_order.local_id = order_map[nRequestId].second;
     //   rtn_order.sys_id = data.m_nGroupID;
     //   rtn_order.decre = 1;
     //   rtn_order.order_status = '3';
     //
     //   memcpy(buf, QMT_RTN_ORDER, sizeof(QMT_RTN_ORDER) );
     //   memcpy(buf + sizeof(QMT_RTN_ORDER), (const char*)&rtn_order, sizeof(rtn_order));
     //   NetManager<Spi, OrderPolicy>::GetInstance().receive_msg_from_td(buf, sizeof(QMT_RTN_ORDER) + sizeof(rtn_order));
    }

    else if (data.m_eStatus == XTFW_TASK_STATUS_CANCELED) {
    
        LFRtnOrderField rtn_order;

        auto& order_map = NetManager<Spi, OrderPolicy>::GetInstance().m_order_map;
        auto& task_map = NetManager<Spi, OrderPolicy>::GetInstance().m_task_map;

        if (order_map.find(nRequestId) != order_map.end()) {
            string key = to_string(data.m_nTaskId) + to_string(data.m_nGroupID);
            task_map[key] = { order_map[nRequestId].first , order_map[nRequestId].second };
        }

      //  rtn_order.order_id = order_map[nRequestId].first;
      //  rtn_order.local_id = order_map[nRequestId].second;
      //  rtn_order.sys_id = data.m_nGroupID;
      //  rtn_order.decre = 1;
      //  rtn_order.order_status = '8';
      //
      //  memcpy(buf, QMT_RTN_ORDER, sizeof(QMT_RTN_ORDER));
      //  memcpy(buf + sizeof(QMT_RTN_ORDER), (const char*)&rtn_order, sizeof(rtn_order));
      //  NetManager<Spi, OrderPolicy>::GetInstance().receive_msg_from_td(buf, sizeof(QMT_RTN_ORDER) + sizeof(rtn_order));
    
    
    }

    if (false) {
    
        LFRtnTradeField rtn_trade;
        memcpy(buf, QMT_TRADE, sizeof(QMT_TRADE));
        memcpy(buf + sizeof(QMT_TRADE), (const char*)&rtn_trade, sizeof(rtn_trade));
        NetManager<Spi, OrderPolicy>::GetInstance().receive_msg_from_td(buf, sizeof(QMT_TRADE) + sizeof(rtn_trade));
    }

//	NetManager<Spi, OrderPolicy>::GetInstance().receive_msg_from_td();

}

// 成交数据主推
void rtn_trade(const CDealDetail& data, const char* accountKey, int nRequestId, const XtError& error) {

#ifdef PRINT_TRADE
    FileAdapter::GetInstance().flush_to_file("policy.log", "[debug] [rtn_trade] " + string("[key]") + string(accountKey) + "msg \n");
#endif

    FileAdapter::GetInstance().flush_to_file("policy.log", "[rtn_trade]  " + string(data.m_strTradeID) + "msg \n");
    FileAdapter::GetInstance().flush_to_file("policy.log", "[rtn_trade nano] :" + to_string(InitData::GetInstance().ns.rdns()) + "\n");


    if (string(accountKey) != string(NetManager<Spi, OrderPolicy>::GetInstance().m_acct.m_strAccountKey)) return;


    FileAdapter::GetInstance().flush_to_file("policy.log", "[rtn_trade]  " + string(data.m_strTradeID) + "msg \n");

    LFRtnTradeField rtn_trade;
    char buf[1024] = {};

    auto& order_map = NetManager<Spi, OrderPolicy>::GetInstance().m_order_map;
    auto& task_map = NetManager<Spi, OrderPolicy>::GetInstance().m_task_map;


    string key = to_string(data.m_nTaskId) + to_string(data.m_nGroupID);

    if (task_map.find(key) != task_map.end()) {
        string key = to_string(order_map[nRequestId].first) + to_string(order_map[nRequestId].second);
        task_map[key] = { data.m_nTaskId , data.m_nGroupID };

        FileAdapter::GetInstance().flush_to_file("policy.log", "[rtn_trade] " + string("find the Requst ") +
            to_string(nRequestId) +
            "[acct]" + string(accountKey) + "msg \n");
    }
    else {
    
        FileAdapter::GetInstance().flush_to_file("policy.log", "[rtn_trade] " + string("can't find the Requst ") +
            to_string(nRequestId) +
            "[acct]" + string(accountKey) + "msg \n");
    
    }


    FileAdapter::GetInstance().flush_to_file("policy.log", "[rtn_order] " + string("order_id ") +
        to_string(task_map[key].first) +
        string("local_id ") +
        to_string(task_map[key].second) +
        "[acct]" + string(accountKey) + "msg \n");



    rtn_trade.order_id = task_map[key].first;
    rtn_trade.local_id = task_map[key].second;
    rtn_trade.sys_id = data.m_nGroupID;
    rtn_trade.price = data.m_dAveragePrice;
    rtn_trade.volume = data.m_nVolume;

    memcpy(buf, QMT_TRADE, sizeof(QMT_TRADE));
    memcpy(buf + sizeof(QMT_TRADE), (const char*)&rtn_trade, sizeof(rtn_trade));
    NetManager<Spi, OrderPolicy>::GetInstance().receive_msg_from_td(buf, sizeof(QMT_TRADE) + sizeof(rtn_trade));

}


void rtn_order(const COrderDetail& data, const char* accountKey, int nRequestId, const XtError& error) {

        #ifdef PRINT_TRADE
         FileAdapter::GetInstance().flush_to_file("policy.log", "[debug] [rtn_order] " + string("[key]") + string(accountKey) + "msg \n");
         FileAdapter::GetInstance().flush_to_file("policy.log", "[debug] [rtn_order] " + string("[key]") + string(NetManager<Spi, OrderPolicy>::GetInstance().m_acct.m_strAccountKey) + "msg \n");
        #endif



 
        if (string(accountKey) !=  string(NetManager<Spi, OrderPolicy>::GetInstance().m_acct.m_strAccountKey)) return;
        

        FileAdapter::GetInstance().flush_to_file("policy.log", "[rtn_order] "+ to_string(data.m_eOrderStatus)  +
                                                            "[acct]" + string(accountKey)  + "msg \n");
        FileAdapter::GetInstance().flush_to_file("policy.log", "[rtn_order nano] :" + to_string(InitData::GetInstance().ns.rdns()) + "\n");


        
   // if (data.m_eOrderStatus == XTFW_TASK_STATUS_RUNNING) {
        LFRtnOrderField rtn_order;
        char buf[1024] = {};

        auto& order_map = NetManager<Spi, OrderPolicy>::GetInstance().m_order_map;
        auto& task_map = NetManager<Spi, OrderPolicy>::GetInstance().m_task_map;
        auto& status_map = NetManager<Spi, OrderPolicy>::GetInstance().m_status_map;
        
        string key = to_string(data.m_nTaskId) + to_string(data.m_nGroupID);



        if (task_map.find(key) != task_map.end()) {
           // string key = to_string(order_map[nRequestId].first) + to_string(order_map[nRequestId].second);
           // task_map[key] = { data.m_nTaskId , data.m_nGroupID };
            FileAdapter::GetInstance().flush_to_file("policy.log", "[rtn_order] " + string("find the Requst ") +
                to_string(nRequestId) +
                "[acct]" + string(accountKey) + "msg \n");
        }
        else {
            FileAdapter::GetInstance().flush_to_file("policy.log", "[rtn_order] " + string("can't find the Requst ") +
                                                                                    to_string(nRequestId) +
                                                                                     "[acct]" + string(accountKey) + "msg \n");
        }


        FileAdapter::GetInstance().flush_to_file("policy.log", "[rtn_order] " + string("order_id ") + 
                                                                                to_string(task_map[key].first) +
                                                                                string("local_id ") +
                                                                                to_string(task_map[key].second) +
                                                                                  "[acct]" + string(accountKey) + "msg \n");

        rtn_order.order_id = task_map[key].first;
        rtn_order.local_id = task_map[key].second;
        rtn_order.sys_id = data.m_nGroupID;
        rtn_order.decre = 1;
        rtn_order.order_status = status_map[data.m_eOrderStatus];

        memcpy(buf, QMT_RTN_ORDER, sizeof(QMT_RTN_ORDER));
        memcpy(buf + sizeof(QMT_RTN_ORDER), (const char*)&rtn_order, sizeof(rtn_order));
        NetManager<Spi, OrderPolicy>::GetInstance().receive_msg_from_td(buf, sizeof(QMT_RTN_ORDER) + sizeof(rtn_order));
  

}



